def buildAsLibrary = project.hasProperty('BUILD_AS_LIBRARY')
def buildAsApplication = !buildAsLibrary
if (buildAsApplication) {
    apply plugin: 'com.android.application'
}
else {
    apply plugin: 'com.android.library'
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def sdl3Dir = properties.getProperty('sdl3.dir')
if (sdl3Dir == null)
{
    throw new IllegalArgumentException("no [sdl3.dir] property in [local.properties] file")
}
android {
    compileSdkVersion 33
    ndkVersion '25.2.9519653'

    defaultConfig {
        if (buildAsApplication) {
            applicationId "org.home.leo"
        }
        minSdkVersion 16
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared", "-DSDL3_SRC_DIR=$sdl3Dir"
                // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
                // abiFilters 'arm64-v8a'
            }
        }
    }
    buildTypes {
        debug {
            debuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    applicationVariants.all { variant ->
        tasks["merge${variant.name.capitalize()}Assets"]
            .dependsOn("externalNativeBuild${variant.name.capitalize()}")
    }
    if (!project.hasProperty('EXCLUDE_NATIVE_LIBS')) {
        sourceSets.main {
            jniLibs.srcDir 'libs'
        }
        externalNativeBuild {
            cmake {
                path '../../CMakeLists.txt'
            }
        }
       
    }
    lintOptions {
        abortOnError false
    }
    namespace 'org.home.leo'

    if (buildAsLibrary) {
        libraryVariants.all { variant ->
            variant.outputs.each { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith(".aar")) {
                    def fileName = "org.libsdl.app.aar"
                    output.outputFile = new File(outputFile.parent, fileName)
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
}
